﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bunian.Core;
using Bunian.Core.BusinessObjects;
using Bunian.Service;
using Bunian.Web.Common.Code;

namespace Bunian.Web.Pages
{
    public partial class pages_profile_individual : PageBase
    {
        private long _passedChildId;
        private Child _passedChild;
        private BeneficiaryService _service;

        protected void Page_Load(object sender, EventArgs e)
        {
            _service = GetPageLifeSpanningObject<BeneficiaryService>();

            InitializePage();

            ucIndividualProfile.CurrentChild = _passedChild;
        }

        private void InitializePage()
        {
            string passedIdRaw = Request.QueryString["cid"];

            if (long.TryParse(passedIdRaw, out _passedChildId))
            {
                IBeneficiary rawBeneficiary = _service.GetBeneficiaryById(_passedChildId);

                if (null != rawBeneficiary && rawBeneficiary is Child)
                {
                    _passedChild = (Child)rawBeneficiary;
                }
                else
                {
                    throw new Exception("Invalid Page Parameters"); //TODO: Modify this later
                }
            }
            else
            {
                throw new Exception("Invalid Page Parameters"); //TODO: Modify this later
            }
        }
    }
}